package n14.first;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

public class F01 {

	public static void main(String[] args) {
//		List<Person>list= new ArrayList<Person>();
//		list.add(new Person("zhangsan",20));
//		list.add(new Person("lisi",22)); 
//		list.add(new Person("wangwu",18));
		List<Person>list=Arrays.asList(
			new Person("zhangsan",20),
			new Person("lisi",22),
			new Person("wangwu",18)
		);
		list.stream().forEach(System.out::println);
		System.out.println("----------------------");
		Consumer<Person>c1=System.out::print;
		Consumer<Person>c2=p->System.out.println("\t"+p.getAge());
		list.stream().forEach(c1.andThen(c2));
		
		System.out.println("年龄最大的人是：");
		list.stream().max((p1,p2)->p1.getAge()-p2.getAge())
			.ifPresent(System.out::println);
		
		list.stream().max(Comparator.comparingInt(p->p.getAge())).ifPresent(System.out::println);
		list.stream().max(Comparator.comparingInt(Person::getAge)).ifPresent(System.out::println);
		list.stream().reduce((p1,p2)->p1.getAge()>p2.getAge()?p1:p2).ifPresent(System.out::println);
		
		System.out.println("统计人的个数：");
		System.out.println(list.size());
		System.out.println(list.stream().count());
		//reduce的初值一定要设置为0，否则结果会出现错误
		System.out.println(list.stream().parallel().reduce(1000,(sum, person)->++sum, (sum1,sum2)->sum1+sum2));
		
		System.out.println("有人年龄大于20吗？："+list.stream().anyMatch(p->p.getAge()>20));
		System.out.println("有人年龄大于30吗？："+list.stream().anyMatch(p->p.getAge()>30));
		
	}

}
